﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using BigEgg.Framework.Foundation;
using FMStudio.Domain.SQLite.Properties;

namespace FMStudio.Domain.SQLite
{
    [MetadataType(typeof(IAuditBill))]
    public partial class AuditBill : IAuditBill, IDataErrorInfo
    {
        #region Implement IDataErrorInfo Interface
        [NonSerialized]
        private readonly DataErrorInfoSupport dataErrorInfoSupport;

        string IDataErrorInfo.Error { get { return this.dataErrorInfoSupport.Error; } }

        string IDataErrorInfo.this[string memberName] { get { return this.dataErrorInfoSupport[memberName]; } }
        #endregion


        public AuditBill()
            : this(0)
        {
        }


        public AuditBill(int index)
        {
            this.dataErrorInfoSupport = new DataErrorInfoSupport(this);

            //  Default value
            this.Date = DateTime.Today;
            this.Index = index;
            this.Price = 1.0;

            this.AuditTypeReference.AssociationChanged += AuditTypeReferenceAssociationChanged;
        }


        #region Implement IAuditBill Interface
        IAuditType IAuditBill.AuditType
        {
            get { return this.AuditType; }
            set { this.AuditType = value as AuditType; }
        }
        #endregion


        #region Private Methods
        private void AuditTypeReferenceAssociationChanged(object sender, CollectionChangeEventArgs e)
        {
            // The navigation property doesn't support the PropertyChanged event. We have to raise it ourselves.
            OnPropertyChanged("AuditType");
            this.AuditTypeID = this.AuditType.ID;
        }
        #endregion
    }
}
